package com.sdcqjy.property.presenter;

import com.google.gson.Gson;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.contract.ChangeContract;

import static com.sdcqjy.mylibrary.http.HttpClient.HttpType.JSON;
import static com.sdcqjy.property.presenter.contract.a.UpdateUserInfo;

/**
 * Created by 磁磁帅 on 2018/4/30.
 * 功能：
 */

public class ChangePresenter extends BaseContract.Presenter<ChangeContract.View> {
    public ChangePresenter(ChangeContract.View baseView) {
        super(baseView);
    }

    public void changeName(String name) {
        AppLL appLL = AppLL.getAppLL();
        LoginMode mode = LoginMode.getMode(appLL);
        new HttpClient()
                .addParam("userName", name)
                .addParam("id", mode.id)
                .setHttpType(JSON).httpRequest(UpdateUserInfo, new BaseRequestBack<BaseRet>() {
            public BaseRet jsonToMode(String jsonStr) {
                return new Gson().fromJson(jsonStr, BaseRet.class);
            }

            public void onResponseSucc(BaseRet ret) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (ret.isOk()) {
                        baseView.changeSucc();
                    } else baseView.applyError(ret.msg);
                }
            }

            public void onResponseFail(String retStr) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.applyError(retStr);
                }
            }
        });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void changeEmail(String name) {
        AppLL appLL = AppLL.getAppLL();
        LoginMode mode = LoginMode.getMode(appLL);
        new HttpClient()
                .addParam("email", name)
                .addParam("id", mode.id)
                .setHttpType(JSON).httpRequest(UpdateUserInfo, new BaseRequestBack<BaseRet>() {
            public BaseRet jsonToMode(String jsonStr) {
                return new Gson().fromJson(jsonStr, BaseRet.class);
            }

            public void onResponseSucc(BaseRet ret) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (ret.isOk()) {
                        baseView.changeSucc();
                    } else baseView.applyError(ret.msg);
                }
            }

            public void onResponseFail(String retStr) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.applyError(retStr);
                }
            }
        });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void changePwd(String oldPwd, String pwd) {
        AppLL appLL = AppLL.getAppLL();
        LoginMode mode = LoginMode.getMode(appLL);
        new HttpClient()
                //.addParam("oldPwd", oldPwd)
                .addParam("passWord", pwd)
                .addParam("id", mode.id)
                .setHttpType(JSON).httpRequest(UpdateUserInfo, new BaseRequestBack<BaseRet>() {
            public BaseRet jsonToMode(String jsonStr) {
                return new Gson().fromJson(jsonStr, BaseRet.class);
            }

            public void onResponseSucc(BaseRet ret) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (ret.isOk()) {
                        baseView.changeSucc();
                    } else baseView.applyError(ret.msg);
                }
            }

            public void onResponseFail(String retStr) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.applyError(retStr);
                }
            }
        });
        if (baseView != null) baseView.openLoadDialog();
    }
}
